package com.tsing.cedap.rest.controller;

import com.tsing.cedap.common.annotations.Limit;
import com.tsing.cedap.common.annotations.LimitType;
import com.tsing.cedap.common.annotations.PassToken;
import com.tsing.cedap.common.annotations.SystemLog;
import com.tsing.cedap.common.utils.ResultUtil;
import com.tsing.cedap.common.vo.PageResult;
import com.tsing.cedap.common.vo.ResultData;
import com.tsing.cedap.rest.service.IAdminService;
import com.tsing.cedap.rest.vo.reqVo.CodeReqVo;
import com.tsing.cedap.rest.vo.reqVo.UpdateUserReqVo;
import com.tsing.cedap.rest.vo.reqVo.UserPageReqVo;
import com.tsing.cedap.rest.vo.reqVo.WebLoginReqVo;
import com.tsing.cedap.rest.vo.respVo.LoginRespVo;
import com.tsing.cedap.rest.vo.respVo.UserPageRespVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

/**
 * @author TheTsing
 * @create 2022-03-01 10:20
 */
@Api(tags = "web端用户模块")
@RestController
@RequestMapping("/admin")
public class WebUserController {

    @Autowired
    private IAdminService adminService;

    @PassToken
    @Limit(period = 60000, count = 1, limitType = LimitType.METHODIP)
    @PostMapping("/sendMessage")
    @ApiOperation(value = "网页端发送短信验证码", notes = "验证码有效期为10分钟，发送间隔1分钟以上")
    @SystemLog(description = "网页端发送短信验证码")
    public ResultData sendMessage(@Validated @RequestBody CodeReqVo reqVo) {
        adminService.sendMessage(reqVo);
        return ResultUtil.success();
    }

    @PassToken
    @Limit(period = 1000, count = 1, limitType = LimitType.METHODIP)
    @PostMapping("/login")
    @ApiOperation(value = "网页端登录")
    @SystemLog(description = "网页端登录")
    public ResultData<LoginRespVo> login(@Validated @RequestBody WebLoginReqVo reqVo) {
        return ResultUtil.success(adminService.login(reqVo));
    }

    @GetMapping("/userPage")
    @ApiOperation(value = "用户分页")
    @SystemLog(description = "用户分页")
    public ResultData<PageResult<UserPageRespVo>> userPage(@Validated UserPageReqVo reqVo) {
        return ResultUtil.success(adminService.userPage(reqVo));
    }

    @GetMapping("/getUser")
    @ApiOperation(value = "获取用户信息")
    @SystemLog(description = "获取用户信息")
    public ResultData<UserPageRespVo> getUser(@Validated CodeReqVo reqVo) {
        return ResultUtil.success(adminService.getUser(reqVo));
    }

    @PutMapping("/updateUser")
    @ApiOperation(value = "修改用户信息")
    @SystemLog(description = "修改用户信息")
    public ResultData updateUser(@Validated @RequestBody UpdateUserReqVo reqVo) {
        adminService.updateUser(reqVo);
        return ResultUtil.success();
    }

    @PutMapping("/updatePwd")
    @ApiOperation(value = "修改用户密码")
    @SystemLog(description = "修改用户密码")
    public ResultData updatePwd(@Validated @RequestBody CodeReqVo reqVo, HttpServletRequest request) {
        adminService.updatePwd(reqVo, request);
        return ResultUtil.success();
    }


}
